


#include "ast.h"
#include "ast_function.h"
#include "ast_def.h"

void ProgramNode::genCode(Synthesizer *out, MemoryAllocator *allocator) {
    if(vardef != nullptr) vardef->genCode(out, allocator, true);
    
    std::vector<Synthesizer*> segs;
    for(auto *x : functions){
        auto *s = new Synthesizer();
        segs.push_back(s);
        auto mem = new MemoryAllocator(s, new Register(), allocator);
        x->genCode(s, mem);
        delete mem;
    }
    mainNode->genCode(out, allocator);
    for(auto x : segs){
        out->concat(x);
        delete x;
    }
}

void MainNode::genCode(Synthesizer *out, MemoryAllocator *allocator) {
    statements->genCode(out, allocator);
    out->text<<"li $v0, 10"<<std::endl;
    out->text<<"syscall"<<std::endl;
}
